package mi.payment.adapter;

import android.content.Context;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseListAdapter<T> extends BaseAdapter {

    public Context mContext;

    private List<T> mList = new ArrayList<>();

    public BaseListAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public T getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public Context getContext() {
        return mContext;
    }

    public void setData(List<T> list) {
        mList.clear();
        if (list != null && list.size() > 0) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void setData(int index, T data) {
        mList.set(index, data);
        notifyDataSetChanged();
    }

    public void addData(T t) {
        mList.add(t);
        notifyDataSetChanged();
    }

    public void addData(int index, T t) {
        mList.add(index, t);
        notifyDataSetChanged();
    }

    public void addData(List<T> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void addData(int index, List<T> list) {
        if (list != null && list.size() > 0) {
            mList.addAll(index, list);
        }
        notifyDataSetChanged();
    }

    public boolean removeData(T data) {
        boolean result = mList.remove(data);
        if (result) {
            notifyDataSetChanged();
        }
        return result;
    }

    public T removeData(int index) {
        T result = mList.remove(index);
        notifyDataSetChanged();
        return result;
    }

    public boolean removeData(List<T> list) {
        boolean result = true;
        if (list != null && list.size() > 0) {
            result = mList.removeAll(list);
        }
        if (result) {
            notifyDataSetChanged();
        }
        return result;
    }

    public List<T> getData() {
        return mList;
    }

    public void clearData() {
        mList.clear();
        notifyDataSetChanged();
    }


}
